home *** CD-ROM | disk | FTP | other *** search
/ Alles Voor Internet / Tout Pour Internet / alles voor internet.iso / MacInternet™ / Telnet / NCSA / tn3270 2.4d7 source / tn3270 / tcpevent.c < prev    next >
Text File  |  1992-04-17  |  10KB  |  404 lines

  1. /*
  2.  *  tn3270 for the Macintosh Source Code
  3.  *  Brown University Computing and Information Services
  4.  *  Version 2.4d7  April, 1992
  5.  *  Copyright (c) 1988, 1989, 1990, 1991, 1992 by Brown University and by
  6.  *  Peter John DiCamillo.
  7.  *
  8.  *  Permission is granted to any individual or institution to use, copy,
  9.  *  or redistribute the binary version of this software and its
  10.  *  documentation provided this notice and the copyright notices are
  11.  *  retained.  Permission is granted to any individual or non-profit
  12.  *  institution to use, copy, modify, or redistribute the source files
  13.  *  of this software provided this notice and the copyright notices are
  14.  *  retained.  This software may not be distributed for profit, either
  15.  *  in original form or in derivative works, nor can the source be
  16.  *  distributed to other than an individual or a non-profit institution.
  17.  *  Any  individual or group interested in seeing and/or using these
  18.  *  source files but who are prevented from doing so by the above
  19.  *  constraints should contact Don Wolfe, Assistant Vice-President for
  20.  *  Computer Systems at Brown University, (401) 863-7250, for possible
  21.  *  software licensing of the source developed at Brown.
  22.  *
  23.  *  Brown University and Peter John DiCamillo make no representations
  24.  *  about the suitability of this software for any purpose.
  25.  *
  26.  *  BROWN UNIVERSITY AND PETER JOHN DICAMILLO GIVE NO WARRANTY, EITHER
  27.  *  EXPRESS OR IMPLIED, FOR THE PROGRAM AND/OR DOCUMENTATION PROVIDED,
  28.  *  INCLUDING, WITHOUT LIMITATION, WARRANTY OF MERCHANTABILITY AND
  29.  *  WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE.
  30.  *
  31.  */
  32.  
  33. #if !defined(USEDUMP)
  34.     #include "maclib.h"
  35.     #include "termdef.h"
  36.     #include "tn3270funcs.h"
  37.     #include "globals.h"
  38. #else
  39.     #pragma load "tn3270DumpFile"
  40. #endif
  41.  
  42. #include "netevent.h"
  43. #include "hostform.h"
  44. #include "NCSAfuncs.h"            /* must follow hostform.h */
  45.  
  46. #pragma segment 3270tcp
  47.  
  48. extern char tcpinitok;        /* network code is active */
  49. extern unsigned char * rcvbuff;
  50. extern short rcvmax;
  51.  
  52. void tcpevent(void)
  53. {
  54. static unsigned long * Ticks = (unsigned long *)0x16a;
  55. int ev, what, data; 
  56. short cnt, i, j;
  57. OSErr rc;
  58. cnr *cp;
  59. short kindex;
  60. unsigned long klimit;
  61. CursHandle watchHandle;
  62. char waitcur;
  63.  
  64. myStask();
  65. ev = Sgetevent(USERCLASS | ERRCLASS | CONCLASS, &what, &data);
  66.  
  67. if (ev) {
  68.     sprintf(dbgmsg, "event = %d, class = %d, data = %d", ev, what, data);
  69.     putln(dbgmsg);
  70.  
  71.     switch(what) {
  72.         case CONCLASS:
  73.             cp = port_to_cp(data);
  74.             if (cp == 0) break;
  75.             switch(ev) {
  76.                 case CONOPEN:
  77.                             if (cp->connstate == 3) {
  78.                                 cp->connstate = 4;
  79.                                 cp->online = 1;
  80.                                 cp->kblcode = 2;
  81.                                 if (cp->myWindow == 0) {
  82.                                     rc = createwindow(cp);
  83.                                     if (rc != noErr) {
  84.                                         tcplgout(0, cp);
  85.                                         }
  86.                                     }
  87.                                 else {
  88.                                     newstat(cp);
  89.                                     }
  90.                                 /*     if the session was opened by the api, we
  91.                                     delay registering the session until data
  92.                                     is received. (See telnet.c)  */
  93.                                 if (!(cp->apiopenpend)) {
  94.                                     apiregister(1, cp);
  95.                                     }
  96.                                 if (cp->serverconn) {
  97.                                     cp->servermode = 0;
  98.                                     srvswitch(cp);
  99.                                     if (!(cp->logon)) {
  100.                                         if (cp->kblock) {
  101.                                             cp->kblock = 0;
  102.                                             newstat(cp);
  103.                                             }
  104.                                         cp->hadascii = 1;
  105.                                         return;
  106.                                         }
  107.                                     }
  108.                                 }
  109.                             break;
  110.                 case CONDATA:
  111.                             cnt = netread(data, rcvbuff+64, rcvmax);
  112.                             myStask();
  113.                             if (cnt == 0) break;
  114.                             if (cnt < 0) {
  115.                                 closeresponse(closeNetFail, cp);
  116.                                 tcplgout(1, cp);
  117.                                 break;
  118.                                 }
  119.                             if (cp->logon) {
  120.                                 cp->wr_active = 1;
  121.                                 newstat(cp);
  122.                                 }
  123.                             while (cnt > 0) {
  124.                                 telrcv(rcvbuff+64, cnt, cp);
  125.                                 myStask();
  126.                                 cnt = netread(data, rcvbuff+64, rcvmax);
  127.                                 myStask();
  128.                                 }
  129.                             if (cp->wr_active && (!(cp->needwrite))) {
  130.                                 cp->wr_active = 0;
  131.                                 newstat(cp);
  132.                                 }
  133.                                                             /* 3270 mode */
  134.                             if (cp->fsinv && (cp->data_init == 1)) {
  135.                                 cp->fsinv = 0;
  136.                                 invldscr(cp);
  137.                                 if (cp->pndbeep) {
  138.                                     cp->pndbeep = 0;
  139.                                     myStask();    /* beep takes a while */
  140.                                     beep(cp);
  141.                                     myStask();
  142.                                     }
  143.                                 }
  144.                                                             /* line mode */
  145.                             else if (cp->logon == 0) {
  146.                                 if (cp->pndbeep) {
  147.                                     cp->pndbeep = 0;
  148.                                     myStask();    /* beep takes a while */
  149.                                     beep(cp);
  150.                                     myStask();
  151.                                     }
  152.                                 }
  153.                             if (cnt < 0) {
  154.                                 closeresponse(closeNetFail, cp);
  155.                                 tcplgout(1, cp);
  156.                                 }
  157.                             break;
  158.                 case CONCLOSE:
  159.                             closeresponse(closeNetClose, cp);
  160.                             tcplgout(1, cp);
  161.                             break;
  162.                 case CONFAIL:
  163.                             if (cp->connstate == 3) {
  164.                                 stoperr(conalrt, cp);
  165.                                 cp->connstate = 0;
  166.                                 cp->kblcode = 4;
  167.                                 if (cp->apiopen) {
  168.                                     apiopenerr(openConnFailed, cp);
  169.                                     }
  170.                                 removeconnection(cp);
  171.                                 }
  172.                             break;
  173.                 default:
  174.                             break;
  175.                 }
  176.             break;
  177.  
  178.         case USERCLASS:
  179.             switch(ev) {
  180.                 case DOMOK:
  181.                             cp = port_to_cp(data);
  182.                             if (cp == 0) break;
  183.                             if (cp->connstate != 2) break;
  184.                             cp->mp = Slooknum(data);
  185.                             if (cp->mp == 0) {
  186.                                 putln("Slooknum failed!");
  187.                                 stoperr(topnalrt, cp);
  188.                                 cp->connstate = 0;
  189.                                 cp->kblcode = 4;
  190.                                 if (cp->apiopen) {
  191.                                     apiopenerr(openConnFailed, cp);
  192.                                     }
  193.                                 removeconnection(cp);
  194.                                 break;
  195.                                 }
  196.                             sprintf(dbgmsg, "address for %s is %d.%d.%d.%d",
  197.                                 (cp->mp)->hname, (cp->mp)->hostip[0],
  198.                                 (cp->mp)->hostip[1], (cp->mp)->hostip[2],
  199.                                 (cp->mp)->hostip[3]);
  200.                             putln(dbgmsg);
  201.                             cp->myport = Snetopen(cp->mp, rdportnum(cp));
  202.                             if (cp->myport < 0) {
  203.                                 putln("Snetopen failed!");
  204.                                 stoperr(topnalrt, cp);
  205.                                 cp->connstate = 0;
  206.                                 cp->kblcode = 4;
  207.                                 if (cp->apiopen) {
  208.                                     apiopenerr(openConnFailed, cp);
  209.                                     }
  210.                                 removeconnection(cp);
  211.                                 break;
  212.                                 }
  213.                             cp->connstate = 3;
  214.                             break;
  215.                 case DOMFAIL:
  216.                             cp = port_to_cp(data);
  217.                             if (cp == 0) break;
  218.                             if (cp->connstate != 2) break;
  219.                             stoperr(numalrt, cp);
  220.                             cp->connstate = 0;
  221.                             cp->kblcode = 4;
  222.                             if (cp->apiopen) {
  223.                                 apiopenerr(openResFailed, cp);
  224.                                 }
  225.                             removeconnection(cp);
  226.                             break;
  227.                 case FTPCOPEN:
  228.                             ftpcopen = 1;
  229.                             ftpnewstat();
  230.                             putln("FTP control connection opened");
  231.                             break;
  232.                 case FTPCLOSE:
  233.                             ftpcopen = 0;
  234.                             ftpnewstat();
  235.                             putln("FTP control connection closed");
  236.                             break;
  237.                 case FTPBEGIN:
  238.                             ftpdopen = 1;
  239.                             ftpnewstat();
  240.                             putln("FTP data connection opened");
  241.                             break;
  242.                 case FTPEND:
  243.                             ftpdopen = ftplopen = 0;
  244.                             ftpnewstat();
  245.                             putln("FTP data or list connection closed");
  246.                             break;
  247.                 case FTPLIST:
  248.                             ftplopen = 1;
  249.                             ftpnewstat();
  250.                             putln("FTP list connection opened");
  251.                             break;
  252.                 case FTPUSER:
  253.                             putln("FTP user name entered");
  254.                             break;
  255.                 case FTPPWOK:
  256.                             putln("FTP password verified");
  257.                             break;
  258.                 case FTPPWNO:
  259.                             putln("FTP password failed");
  260.                             break;
  261.                 case RCPBEGIN:
  262.                             putln("rcp transfer begin");
  263.                             break;
  264.                 case RCPEND:
  265.                             putln("rcp transfer ended");
  266.                             break;
  267.                 default:
  268.                             putln("received unknown user class event");
  269.                             break;
  270.                 }
  271.             break;
  272.         case ERRCLASS:
  273.             NetError(data);
  274.             break;
  275.         case ICMPCLASS:
  276.             putln("received ICMP event (should never happen)");
  277.             break;
  278.         case SCLASS:
  279.             putln("received session event (should never happen)");
  280.             break;
  281.         default:
  282.             putln("received event with unknown class");
  283.             break;
  284.         }
  285.     myStask();
  286.     }
  287.  
  288. for (j=0; j < MAXSESSIONS; j++) {
  289.     cp = cplist[j];
  290.     if (cp != 0) {
  291.         if (cp->tcpflg) {
  292.             kindex = 0;
  293.             klimit = (*Ticks) + 15;
  294.             waitcur = 0;
  295.             if (cp->pasteactive == 0) {
  296.                 while ((cp->kbqsize > 0) && (!(cp->kblock))) {
  297.                     if (cp->vmxbgn && (!(cp->vmxsub))) break;
  298.                     if (cp->pasteactive) break;
  299.                     if (((cp->kbqueue)[kindex].shift) == 100) {
  300.                         cp->pasteactive = 1;
  301.                         }
  302.                     else {
  303.                         hndkbd((cp->kbqueue)[kindex].code,
  304.                                (cp->kbqueue)[kindex].shift, cp);
  305.                         }
  306.                     kindex++;
  307.                     cp->kbqsize--;
  308.                     if ((*Ticks) > klimit) {
  309.                         myStask();
  310.                         klimit = (*Ticks) + 15;
  311.                         if (waitcur == 0) {
  312.                             watchHandle = GetCursor(4);
  313.                             SetCursor(*watchHandle);
  314.                             waitcur = 1;
  315.                             dfltcurs = 2;
  316.                             }
  317.                         }
  318.                     }
  319.                 if ((cp->kbqsize > 0) && (kindex > 0)) {
  320.                     for (i = 0; i < cp->kbqsize; i++) {
  321.                         (cp->kbqueue)[i].code = (cp->kbqueue)[i+kindex].code;
  322.                         (cp->kbqueue)[i].shift = (cp->kbqueue)[i+kindex].shift;
  323.                         }
  324.                     }
  325.                 }
  326.             else {        /* pasteactive true */
  327.                 while ((cp->pastebuffsize > 0) && (!(cp->kblock))) {
  328.                     if (cp->vmxbgn && (!(cp->vmxsub))) break;
  329.                     hndkbd((cp->pastebuff)[kindex], 99, cp);
  330.                     kindex++;
  331.                     cp->pastebuffsize--;
  332.                     if ((*Ticks) > klimit) {
  333.                         myStask();
  334.                         klimit = (*Ticks) + 15;
  335.                         if (waitcur == 0) {
  336.                             watchHandle = GetCursor(4);
  337.                             SetCursor(*watchHandle);
  338.                             waitcur = 1;
  339.                             dfltcurs = 2;
  340.                             }
  341.                         }
  342.                     }
  343.                 if ((cp->pastebuffsize > 0) && (kindex > 0)) {
  344.                     for (i = 0; i < cp->pastebuffsize; i++) {
  345.                         (cp->pastebuff)[i] = (cp->pastebuff)[i+kindex];
  346.                         }
  347.                     }
  348.                 if (cp->pastebuffsize == 0) {
  349.                     cp->pasteactive = 0;
  350.                     DisposPtr(cp->pastebuff);
  351.                     cp->pastebuff = 0;
  352.                     if (cp->pastebeep) beep(cp);
  353.                     newwrite(cp);
  354.                     newcur(cp);
  355.                     }
  356.                 }
  357.             }
  358.         }
  359.     }
  360. }
  361.  
  362. void myStask(void)
  363. {
  364. if (tcpinitok) {
  365.     Stask();
  366.     if (ftpcopen || ftpdopen || ftplopen) {
  367.         rftpd(0);
  368.         }
  369.     }
  370. }
  371.  
  372. cnr *port_to_cp(int port)
  373. {
  374. short i;
  375. cnr *cp;
  376.  
  377. for (i=0; i < MAXSESSIONS; i++) {
  378.     cp = cplist[i];
  379.     if (cp != 0) {
  380.         if (cp->tcpflg && (cp->connstate > 0) && (cp->myport == port)) {
  381.             return(cp);
  382.             }
  383.         }
  384.     }
  385. sprintf(dbgmsg, "no connection record found for port %d", port);
  386. putln(dbgmsg);
  387. return(0);
  388. }
  389.  
  390. void ftpnewstat(void)
  391. {                                /* newstat for all windows */
  392. short i;
  393. cnr *cp;
  394.  
  395. for (i=0; i < MAXSESSIONS; i++) {
  396.     cp = cplist[i];
  397.     if (cp != 0) {
  398.         if (cp->myWindow != 0) {
  399.             newstat(cp);
  400.             }
  401.         }
  402.     }
  403. }
  404.